package com.wshl.aijiuunion.utils;

import android.app.Activity;

import com.hjq.permissions.OnPermission;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.wshl.aijiuunion.GlobConstant;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.photopicker.activity.BGAPhotoPreviewActivity;

/**
 * Created buy 风凊扬 on 2019/6/27 0027
 */
public class ImagePreviewUtils {

    public static void previewImg(final Activity context, final List<String> imgurls, final int position) {

        XXPermissions.with(context)
                .constantRequest() //设置被拒绝后继续申请，直到用户授权或者永久拒绝
                // .permission(Permission.SYSTEM_ALERT_WINDOW, Permission.REQUEST_INSTALL_PACKAGES) //支持请求6.0悬浮窗权限8.0请求安装权限
                .permission(Permission.Group.STORAGE) //不指定权限则自动获取清单中的危险权限
                .request(new OnPermission() {
                    @Override
                    public void hasPermission(List<String> granted, boolean isAll) {
                        //点击图片预览
                        File downloadDir = new File(GlobConstant.DCIM_DIR);
                        BGAPhotoPreviewActivity.IntentBuilder photoPreviewIntentBuilder = new BGAPhotoPreviewActivity.IntentBuilder(context)
                                .saveImgDir(downloadDir); // 保存图片的目录，如果传 null，则没有保存图片功能
                        if (imgurls.size() == 1) {
                            // 预览单张图片
                            photoPreviewIntentBuilder.previewPhoto(imgurls.get(0));
                        } else if (imgurls.size() > 1) {
                            // 预览多张图片
                            photoPreviewIntentBuilder.previewPhotos((ArrayList<String>) imgurls)
                                    .currentPosition(position); // 当前预览图片的索引
                        }
                        context.startActivity(photoPreviewIntentBuilder.build());
                    }

                    @Override
                    public void noPermission(List<String> denied, boolean quick) {

                    }
                });

    }




}
